package org.backend.canteenbackend.controller;

import org.backend.canteenbackend.common.AuthResponse;
import org.backend.canteenbackend.common.CommonResponse;
import org.backend.canteenbackend.security.JwtUtil;
import org.backend.canteenbackend.service.GiteeOAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private GiteeOAuthService giteeOAuthService;

    @GetMapping("/gitee")
    public CommonResponse<AuthResponse> authenticate(@RequestParam String code) {
        String githubUsername = giteeOAuthService.getGiteeUsername(code);
        String jwtToken = jwtUtil.generateToken(githubUsername);
        return CommonResponse.createForSuccess(new AuthResponse(jwtToken, "Bearer"));
    }
}